package com.ruoyi.system.netty;

import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import com.ruoyi.common.core.domain.ProtoCode;
import com.ruoyi.common.core.utils.EncryptAesUtil;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import proto.client.*;

import java.nio.charset.StandardCharsets;

/**
 * Author:cgChen
 * Date:2022/05/14 15:38
 * Description:客户端处理器
 */
@Service("NettyClientHandler")
public class NettyClientHandler extends ChannelInboundHandlerAdapter {
    private static Logger log = LoggerFactory.getLogger(NettyClientHandler.class);
    //与服务端建立连接后得到的通道对象
    private Channel channel;
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        log.info("客户端Active .....");
        channel=ctx.channel();
        //UserInfo.Login.Builder userMsg=UserInfo.Login.newBuilder();
        //userMsg.setPhone("123123123");
        //userMsg.setPassword("123");
        //ProtoCmd.RequestCmd.Builder cmd=ProtoCmd.RequestCmd.newBuilder();
        //cmd.setPact(0x01);
        //cmd.setCode(101);
        //cmd.setData(userMsg.build().toByteString());
        //ctx.write(cmd.build());
        //ctx.flush();




        //UserInfo.SendSms.Builder user=UserInfo.SendSms.newBuilder();
        //user.setTelephone("15926689137");
        //user.setType(1);
        //cmd.setCode(104);


        //UserInfo.Register.Builder user=UserInfo.Register.newBuilder();
        //user.setUserName("dushuai");
        //user.setPhone("15926689137");
        //user.setVerifyCode("620480");
        //user.setPassword("123456");
        //user.setRecommendUserId("123123");
        //user.setParentId("123123");
        //cmd.setCode(102);
//String token="eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoyLCJ1c2VyX2tleSI6IjE5OTAyYTIzLTFhYzAtNDk5ZS04NTdjLTFkNzhiZTRlNmZlNyIsInVzZXJuYW1lIjpudWxsfQ.QjdkJknbpsOThDwy5_ybdQ-8w65Ak3ZDYIPtwc4Fa5rnYr0SEisUFmUKlYbT0GAt5Ok1AXzf6XgdJsCR0aLo-A";
        ProtoCmd.RequestCmd.Builder cmd=ProtoCmd.RequestCmd.newBuilder();
//        String token="eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoxMzg1LCJ1c2VyX2tleSI6IjIyOWY4Y2I1LTcyZGEtNDg3Zi05ZmYxLWY0Nzc2YzhhYjEyYyIsInVzZXJuYW1lIjpudWxsfQ.arQWDVDigeJK9sURcUEipu00aYrPGf5FX4U8_N3pohFEF6tUboNERUoxyLF9G6pt5AuDfVLQwvgnlxtU1RxNRQ";
//        OperationCenter.RequestTransferOperation.Builder user=OperationCenter.RequestTransferOperation.newBuilder();
//        user.setType(1);
//        user.setTelephone("15071272007");
//        user.setPrice(100);
//        user.setPayPass("1");
//        user.setToken(token);
        //eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoxMzg1LCJ1c2VyX2tleSI6IjIyOWY4Y2I1LTcyZGEtNDg3Zi05ZmYxLWY0Nzc2YzhhYjEyYyIsInVzZXJuYW1lIjpudWxsfQ.arQWDVDigeJK9sURcUEipu00aYrPGf5FX4U8_N3pohFEF6tUboNERUoxyLF9G6pt5AuDfVLQwvgnlxtU1RxNRQ

        // 登录
        //UserInfo.Login.Builder user=UserInfo.Login.newBuilder();
        //user.setPhone("18986330988");//18986330988
        //user.setPassword("mj632128");//mj632128

        GoodsGroup.RequestReserveGroup.Builder group=GoodsGroup.RequestReserveGroup.newBuilder();
        group.setToken("eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoxMzg1LCJ1c2VyX2tleSI6IjIyOWY4Y2I1LTcyZGEtNDg3Zi05ZmYxLWY0Nzc2YzhhYjEyYyIsInVzZXJuYW1lIjpudWxsfQ.arQWDVDigeJK9sURcUEipu00aYrPGf5FX4U8_N3pohFEF6tUboNERUoxyLF9G6pt5AuDfVLQwvgnlxtU1RxNRQ");
        group.setType(1);
        group.setGoodsGroupId(1);
        cmd.setCode(101);
        cmd.setData(group.build().toByteString());

        // 编辑收货地址
//        SystemSetting.RequestAddress.Builder address = SystemSetting.RequestAddress.newBuilder();
//        address.setType(2);
//        address.setId(4);
//        address.setProvinceName("湖北");
//        address.setCityName("武汉");
//        address.setCountyName("洪山区");
//        address.setAddress("光谷国际总部6-201");
//        address.setName("收货人0714");
//        address.setTelephone("12345678900");
//        address.setDefaultStatus(1);
//        address.setToken("eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoxMzg1LCJ1c2VyX2tleSI6ImY0MDhmZjM0LTJkNTItNDdjZC1hYzliLWE2ZjlmNjBkMzUxNCIsInVzZXJuYW1lIjpudWxsfQ.aaDr8NXJYCWw1ksyagH-S8I-jwCzI089aYRm_LlXMdQnOhfclBJ23yJIzDvO2RCIEVP19yIgURB9cgL_jt6Liw");
//
//        cmd.setCode(410);
//        cmd.setData(address.build().toByteString());


        // 商品
//        GoodsGroup.RequestReserveGroup.Builder group=GoodsGroup.RequestReserveGroup.newBuilder();
//        group.setType(2);
//        group.setGoodsGroupId(1);
//        group.setToken("eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoxMzg1LCJ1c2VyX2tleSI6IjI4NDM2NzA0LTRjNDEtNDRmYi05OGQ3LWI0NjljN2E3NWRhNiIsInVzZXJuYW" +
//                "1lIjpudWxsfQ.JZyi0jeWefhw63E2zJnkqckYegmp1bEw3ibHIW7Nkig3PH8og6KZgo4pw9BxLjJDBc6GKB4o1wvUSEzpxsbf6w");

//        cmd.setCode(302);
//        cmd.setData(group.build().toByteString());


        cmd.setPact(0x01);
//        sendMsg(cmd.build());
        //ctx.write(cmd.build());
        //ctx.flush();
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        log.info("客户端收到消息: {}", msg.toString());
        ProtoCmd.ResponseCmd response= (ProtoCmd.ResponseCmd) msg;

        //UserInfo.ResponseLogin userMsg= UserInfo.ResponseLogin.parseFrom(response.getResult());
        log.info("服务器收到消息: {}", response.getMsg());
    }
    /**
     * 向服务器发送消息
     *
     * @param msg
     * @throws Exception
     */
    public void sendMsg(Object msg) throws Exception {
        if (channel != null) {
            channel.writeAndFlush(msg).sync();
            log.info("消息发送成功！");
        } else {
            log.warn("消息发送失败,连接尚未建立!");
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }

    public static void main(String[] args) throws InvalidProtocolBufferException {
//        ProtoCmd.RequestCmd.Builder cmd=ProtoCmd.RequestCmd.newBuilder();
//        UserInfo.Login.Builder user=UserInfo.Login.newBuilder();
//        user.setPhone("15926689137");
//        user.setPassword("123456");
//        cmd.setCode(101);
//
//        cmd.setPact(0x01);
//        cmd.setData(user.build().toByteString());
//        ByteString str=cmd.build().toByteString();
//        UserInfo.Login login=UserInfo.Login.parseFrom(str);
//        System.out.println(EncryptAesUtil.md5("1"));
        NettyClient nettyClient = new NettyClient();
        nettyClient.start();
    }
}
